package com.cet.spring_batch_study.jobs.fileDelete;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobScope;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.transaction.PlatformTransactionManager;

/**
 * @author NSOC
 */
@Configuration
public class FileDeletingJobConfig {

    @Bean
    public Job fileDeletingJob(JobRepository jobRepository, Step deleteFilesInDirStep) {
        return new JobBuilder("fileDeleting", jobRepository)
                .start(deleteFilesInDirStep)
                .build();
    }

    @Bean
    @JobScope
    public Step deleteFilesInDirStep(JobRepository jobRepository,
                                     PlatformTransactionManager transactionManager,
                                     @Value("#{jobParameters['dir.path']}") String dirPath) {
        FileDeletingTasklet fileDeletingTasklet = new FileDeletingTasklet();
        fileDeletingTasklet.setDirectoryResource(new FileSystemResource(dirPath));
        return new StepBuilder("deleteFilesInDirStep", jobRepository)
                .tasklet(fileDeletingTasklet, transactionManager)
                .build();
    }
}
